package com.Business.service;

import com.Base.IdCenter;
import com.Business.dao.ProjectDao;
import com.Pojo.Building;
import com.Pojo.Land;
import com.Pojo.Project;
import com.Pojo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by XingYang on 2019/3/13.
 */
@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    ProjectDao projectDao;
    @Autowired
    BuildingService buildingService;
    @Autowired
    LandService landService;
    @Autowired
    IdCenter idCenter;
    @Override
    public String AddProjectInfo(Project project) {
        String projectId = idCenter.getProjectId(Integer.valueOf(project.getIsremoved()));
        project.setProjectno(projectId);
        projectDao.AddProjectInfo(project);
        List<Building> buildings = project.getBuilding();
        List<Land> lands = project.getLand();
        Map map = new HashMap();
        for(Building building:buildings){
            String bid = "";
            bid = idCenter.getObjId(projectId,idCenter.Building);
            building.setId(bid);
            map.put("projectId",projectId);
            map.put("buildingId",bid);
            buildingService.AddBuilding(building);
            buildingService.BindProject(map);
            map.clear();
        }

        for(Land land:lands){
            String lid = "";
            lid = idCenter.getObjId(projectId,idCenter.Land);
            land.setId(lid);
            map.put("projectId",projectId);
            landService.AddLand(land);
            map.put("landId",lid);
            landService.BindProject(map);
            map.clear();
        }

        return projectId;
    }

    @Override
    public void BindLand(Map<String, String> map) {

    }

    @Override
    public void BindBuilding(Map<String, String> map) {

    }

    @Override
    public void BindAppendage(Map<String, String> map) {

    }

    @Override
    public List<Project> getProjectByUserId(String uid) {
        return null;
    }

    @Override
    public List<Project> getProjectByManagerId(String mid) {
        return null;
    }

    @Override
    public Project getProjectByProjectNo(String projectNo) {
        return projectDao.getProjectByProjectNo(projectNo);
    }

    @Override
    public Map getProjectBaseInfByNo(String projectNo) {
        return projectDao.getProjectBaseInfByNo(projectNo);
    }

    @Override
    public Map<String,Object> getProjectInfByProjectNo(String projectNo) {
        Map<String,Object> map = new HashMap<>();

        Map project =  projectDao.getProjectBaseInfByNo(projectNo);

        map.put("projectInf",project);
        List<Map> list = new ArrayList<>();
        list.addAll(landService.getLandsInfByProjectNo(projectNo));
        list.addAll(buildingService.getBuildingsInfByProjectNo(projectNo));

        map.put("landInf",list);

        return map;
    }

    @Override
    public PageInfo getProjectList(Map param, Subject currentUser, User user) {
        int pageNum = Integer.parseInt( param.get("pageNum").toString());
        int pageSize = Integer.parseInt( param.get("pageSize").toString());
        int state = Integer.parseInt( param.get("state").toString());
        PageHelper.startPage(pageNum,pageSize);

        PageInfo pageInfo = new PageInfo();
        //从数据库查到值后直接返回 防止单用户具有多种角色时 数据覆盖
        if(state==3) {//查询全部

            //管理员角色  查询该角色创建的所有项目
            if (currentUser.hasRole("1")) {
                pageInfo = new PageInfo(projectDao.getProjectByUserId(user.getId()));
                return pageInfo;
            }
            //项目经理角色  查询该项目经理负责的所有项目
            if (currentUser.hasRole("2")) {
                pageInfo = new PageInfo(projectDao.getProjectByManagerId(user.getId()));
                return pageInfo;
            }
            //评估员角色 查询所属主管的所有项目
            if (currentUser.hasRole("3")) {
                pageInfo = new PageInfo(projectDao.getProjectByManagerId(user.getManageraccount()));
                return pageInfo;
            }
            //案例管理员 查询所属主账号创建的所有角色
            if (currentUser.hasRole("4")) {
                pageInfo = new PageInfo(projectDao.getProjectByUserId(user.getMainaccount()));
                return pageInfo;
            }
        }
        else{
            switch (state){
                case 0:
                case 1:
                case 2:
                    //管理员角色  查询该角色创建的所有项目
                    if (currentUser.hasRole("1")) {
                        param.put("uid",user.getId());
                        pageInfo = new PageInfo(projectDao.getProjectByUserIdState(param));
                        return pageInfo;
                    }
                    //项目经理角色  查询该项目经理负责的所有项目
                    if (currentUser.hasRole("2")) {
                        param.put("mid",user.getId());
                        pageInfo = new PageInfo(projectDao.getProjectByManagerIdState(param));
                        return pageInfo;
                    }
                    //评估员角色 查询所属主管的所有项目
                    if (currentUser.hasRole("3")) {
                        param.put("mid",user.getManageraccount());
                        pageInfo = new PageInfo(projectDao.getProjectByManagerIdState(param));
                        return pageInfo;
                    }
                    //案例管理员 查询所属主账号创建的所有角色
                    if (currentUser.hasRole("4")) {
                        param.put("uid",user.getMainaccount());
                        pageInfo = new PageInfo(projectDao.getProjectByUserIdState(param));
                        return pageInfo;
                    }
                    break;
                default: return null;

            }
        }

        return pageInfo;
    }

    @Override
    public void delProject(Map map) {
        projectDao.delProject(map);
    }

    @Override
    public void modifyProjectProgress(Map map) {
        projectDao.modifyProjectProgress(map);
    }

    /**
     * 修改对象评估进度 map中需包含key objId status
     * @param map
     */
    @Override
    public void changeObjStatus(Map map) {
        projectDao.changeObjStatus(map);
    }
}
